"""
Case Type   : 关键字
Case Name   : force index 兼容 primary
Create At   : 2024.04.15
Owner       : qumin
Description :
    1.建有主键的表
    2.创建索引
    3.查询建表语句
    4.select语句中使用force index (primary)进行指定强制走主键索引
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.清理成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Keyword(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_primary_0035'
        self.tab_name = 't_primary_0035'
        self.idx_name = 'ind_primary_0035'

    def test_create_function(self):
        text = '-----创建兼容b库库;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      sql_res, '执行失败:' + text)
        text = '-----step1:建有主键的表; expect:成功-----'
        self.log.info(text)
        sql_cmd1 = f"drop table if exists {self.tab_name};" \
                   f"create table {self.tab_name}(id int not null, " \
                   f"stm char(10)character set 'utf8' collate " \
                   f"utf8mb4_general_ci,primary key(id));"
        self.log.info(sql_cmd1)
        res1 = self.sh_primary.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(res1)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS,
                      res1, '执行失败:' + text)
        text = '-----step2:创建索引; expect:成功-----'
        self.log.info(text)
        sql_cmd2 = f"create index {self.idx_name} on {self.tab_name} " \
                   f"using btree(stm) tablespace pg_default;"
        self.log.info(sql_cmd2)
        res2 = self.sh_primary.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(res2)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS,
                      res2, '执行失败:' + text)
        text = '-----step3:查询建表语句; expect:成功-----'
        self.log.info(text)
        sql_cmd3 = f"show create table {self.tab_name};"
        self.log.info(sql_cmd3)
        res3 = self.sh_primary.execut_db_sql(sql_cmd3, dbname=self.db_name)
        self.log.info(res3)
        self.assertIn('PRIMARY KEY USING btree', res3, '执行失败:' + text)
        text = '-----step4:select语句中使用force index (primary)进行指定强制走主键索引; ' \
               'expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f"select * from {self.tab_name} force index (primary);"
        self.log.info(sql_cmd4)
        res4 = self.sh_primary.execut_db_sql(sql_cmd4, dbname=self.db_name)
        self.log.info(res4)
        self.assertIn('id', res4, '执行失败:' + text)
        self.assertIn('stm', res4, '执行失败:' + text)

    def tearDown(self):
        text = '-----step5:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      sql_res4, '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
